package com.ccp.dev.qbdms.webservice;

import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * webService
 * @author y
 */
@Component
@WebService(serviceName = "CommonWebService", // 与接口中指定的name一致
        targetNamespace = "http://qbdms.dev.ccp.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.ccp.dev.qbdms.webservice.CommonWebService" // 接口地址
)
public class CommonWebServiceImpl implements CommonWebService {
    private static final Log logger = LogFactory.getLog(CommonWebServiceImpl.class);
    @Override
    synchronized public String invoke(String paramMap) {
        Map map;
        try {
            map = JSONObject.parseObject(paramMap, Map.class);
        }catch (Exception e){
            logger.debug("Parameter parsing error。");
            return JSONObject.toJSONString(ResultData.error("Parameter parsing error。"));
        }
        String serviceName = null == map.get("web_serviceName") ? "" : map.get("web_serviceName") .toString() ;
        String methodName = null == map.get("web_methodName") ? "" : map.get("web_methodName") .toString() ;
        if(StringUtil.isEmpty(serviceName)){
            logger.debug("param serviceName cannot be null。");
            return JSONObject.toJSONString(ResultData.error("param serviceName cannot be null。"));
        }
        if(StringUtil.isEmpty(methodName)){
            logger.debug("param methodName cannot be null。");
            return JSONObject.toJSONString(ResultData.error("param methodName cannot be null。"));
        }
        Object baseService = AppUtil.getBean(serviceName);
        if(BeanUtils.isEmpty(baseService)){
            logger.debug("serviceName:"+serviceName+" is not defined。");
            return JSONObject.toJSONString(ResultData.error("serviceName:"+serviceName+" is not defined。"));
        }
        Method[] methods = baseService.getClass().getMethods();
        for(Method method : methods){
            if(method.getName().toLowerCase().equals(methodName.toLowerCase())){
                try {
                    if(map.size()>2){
                        return JSONObject.toJSONString(method.invoke(baseService, map));
                    }else{
                        return JSONObject.toJSONString(method.invoke(baseService));
                    }
                } catch (Exception e) {
                    logger.debug("method:"+methodName+" call failed。");
                    return JSONObject.toJSONString(ResultData.error("method:"+methodName+" call failed。"));
                }
            }
        }
        logger.debug("method:"+methodName+" call failed。");
        return JSONObject.toJSONString(ResultData.success("method:"+methodName+" call failed。"));
    }
}
